package com.yf.zzmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.exception.ServiceException;
import com.yf.zzmusic.mapper.MusicCollectMapper;
import com.yf.zzmusic.pojo.dto.album.AlbumPageDTO;
import com.yf.zzmusic.pojo.dto.album.AlbumUpdateDTO;
import com.yf.zzmusic.pojo.entity.Album;
import com.yf.zzmusic.mapper.AlbumMapper;
import com.yf.zzmusic.pojo.entity.Music;
import com.yf.zzmusic.service.IAlbumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 专辑表 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-07
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements IAlbumService {
  @Autowired
  AlbumMapper albumMapper;

  @Override
  public boolean updateAlbum(AlbumUpdateDTO albumUpdateDTO) {
    if (super.getById(albumUpdateDTO.getAlbumId()) == null) {
      throw new ServiceException(Constants.NO_CONTENT, "专辑未找到");
    }
    Album album = new Album();
    //拷贝
    BeanUtils.copyProperties(albumUpdateDTO, album);
    return updateById(album);
  }

  @Override
  public PageInfo albumByPage(AlbumPageDTO albumPageDTO) {
    QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
    Integer pageNum = albumPageDTO.getPageNum();
    Integer pageSize = albumPageDTO.getPageSize();
    Integer albumId = albumPageDTO.getAlbumId();
    String albumName = albumPageDTO.getAlbumName();
    String albumSingerName = albumPageDTO.getAlbumSingerName();
    Integer albumSingerId = albumPageDTO.getAlbumSingerId();
    Integer areaType = albumPageDTO.getAreaType();
    // 判断pageNum与pageSize
    if (pageNum == null) {
      pageNum = 0;
    }
    if (pageSize == null) {
      pageSize = 1000;
    }
    if (!StringUtils.isEmpty(albumName)) {
      queryWrapper.like("album_name", albumName);
    }
    if (!StringUtils.isEmpty(albumSingerName)) {
      queryWrapper.like("album_singer_name", albumSingerName);
    }
    if (albumId != null) {
      queryWrapper.eq("album_id", albumId);
    }
    if (albumSingerId != null) {
      queryWrapper.eq("album_singer_id", albumSingerId);
    }
    if (areaType != null) {
      queryWrapper.eq("area_type", areaType);
    }
    Page<Album> albumPage = page(new Page<>(pageNum, pageSize), queryWrapper);
    PageInfo<Album> pageInfo = new PageInfo<>();
    pageInfo.setPageNum(pageNum);
    pageInfo.setPageSize(pageSize);
    pageInfo.setTotal(albumPage.getTotal());
    pageInfo.setData(albumPage.getRecords());
    return pageInfo;
  }
}
